package com.jinxin.platform.cdsideparkmgr.utils;

import com.alibaba.fastjson.JSONObject;
import com.jinxin.platform.cdsideparkmgr.bean.enums.ResponseCode;
import org.springframework.util.StringUtils;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-02-27
 * @Author: Lingnan
 **/
public class CommonUtil {

    public static String getDataFromResult(String result){
        //{"code":"0","r1":"{\"Response\": {\t\"ResponseURL\": \"/LAPI/V1.0/PACS/Controller/RemoteOpened\",\t\"CreatedID\": -1, \t\"ResponseCode\": 0, \t\"SubResponseCode\": 0, \t\"ResponseString\": \"Succeed\",\t\"StatusCode\": 0,\t\"StatusString\": \"Succeed\",\t\"Data\": \"null\"\t}}"}
        if(StringUtils.isEmpty(result)){return "";}
        JSONObject returnJson = JSONObject.parseObject(result);
        JSONObject responseJson = JSONObject.parseObject(returnJson.getString("r1"));
        JSONObject response = responseJson.getJSONObject("Response");
        Integer responseCode = response.getInteger("ResponseCode");
        if(responseCode != null && responseCode == 0){
            return response.getString("Data");
        }
        return "";
    }
    public static String getErrorMsgByResult(String result){
        //{"code":"0","r1":"{\"Response\": {\t\"ResponseURL\": \"/LAPI/V1.0/PACS/Controller/RemoteOpened\",\t\"CreatedID\": -1, \t\"ResponseCode\": 0, \t\"SubResponseCode\": 0, \t\"ResponseString\": \"Succeed\",\t\"StatusCode\": 0,\t\"StatusString\": \"Succeed\",\t\"Data\": \"null\"\t}}"}
        if(StringUtils.isEmpty(result)){return "";}
        JSONObject returnJson = JSONObject.parseObject(result);
        JSONObject responseJson = JSONObject.parseObject(returnJson.getString("r1"));
        if(responseJson == null){
            return "";
        }
        JSONObject response = responseJson.getJSONObject("Response");
        if(response == null){return "";}
        Integer responseCode = response.getInteger("ResponseCode");
        if(responseCode == null){return "";}
        return ResponseCode.getDesc(responseCode);
    }

    public static void main(String[] args) {
//        System.out.println(getErrorMsgByResult("{\"code\":\"0\",\"r1\":\"{\\\"Response\\\": {\\t\\\"ResponseURL\\\": \\\"/LAPI/V1.0/PeopleLibraries/4/People\\\",\\t\\\"CreatedID\\\": -1, \\t\\\"ResponseCode\\\": 1, \\t\\\"SubResponseCode\\\": 0, \\t\\\"ResponseString\\\": \\\"Common Error\\\",\\t\\\"StatusCode\\\": 1020,\\t\\\"StatusString\\\": \\\"Illegal Face Photo Length\\\",\\t\\\"Data\\\": \\\"null\\\"\\t}}\"}"));
    }
    public static boolean isSucceed(String result){
        if(StringUtils.isEmpty(result)){return false;}
        JSONObject returnJson = JSONObject.parseObject(result);
        JSONObject responseJson = JSONObject.parseObject(returnJson.getString("r1"));
        if(responseJson == null){
            return false;
        }
        JSONObject response = responseJson.getJSONObject("Response");
        Integer responseCode = response.getInteger("ResponseCode");
        return responseCode != null && responseCode == 0;
    }
}
